package com.wenjie.controller.sys;

import com.wenjie.original.entity.common.vo.RestBaseVo;
import com.wenjie.original.entity.vo.SysCodeTemp.SysCodeTempVo;
import com.wenjie.service.IGenService;
import com.wenjie.vo.gen.PreviewCodeInfo;
import com.wenjie.vo.gen.PreviewCodeInfos;
import com.wenjie.vo.gen.TableInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api("代码生成接口")
@RestController
@RequestMapping("/api/gen")
public class SysCodeGenController {

    @Autowired
    private IGenService genService;

    @ApiOperation("获取数据库全部的表")
    @PostMapping("/getTables")
    public RestBaseVo<TableInfo> getTables() {
        // 获取数据中全部的表信息
        return genService.getTables();
    }


    @ApiOperation("查看表详情信息")
    @PostMapping("/showStructure")
    public String showStructure(@RequestBody String name) {
        // 获取数据中全部的表信息
        if (name.indexOf("\"") == 0) name = name.substring(1);
        if (name.lastIndexOf("\"") == name.length() - 1) name = name.substring(0, name.length() - 1);
        return genService.showStructure(name);
    }

    @ApiOperation("查看一下预览代码")
    @PostMapping("/getPreviewInfo")
    public RestBaseVo<SysCodeTempVo.ResVo> getPreviewInfo(@RequestBody PreviewCodeInfo previewCodeInfo) {
        return genService.genGroup(previewCodeInfo.getTableName(), previewCodeInfo.getSysCodeTempGroupId());
    }


    @ApiOperation("生成代码模版")
    @PostMapping("/genCode")
    public List<SysCodeTempVo.ResVo> genCode(@RequestBody PreviewCodeInfos previewCodeInfos) {

        List<SysCodeTempVo.ResVo> resList = new ArrayList<>();
        for (String tableName : previewCodeInfos.getTableNames()) {
            RestBaseVo<SysCodeTempVo.ResVo> resVoRestBaseVo = genService.genGroup(tableName, previewCodeInfos.getSysCodeTempGroupId());
            resList.addAll(resVoRestBaseVo.getList());
        }
        return resList;
    }


}
